package com.sf.ams.sqlfile;

import java.util.List;

import com.sf.ams.constant.Constant;
import com.sf.ams.entity.Environment;
import com.sf.ams.entity.SystemInfo;
import com.sf.ams.entity.enums.SystemExceptionType;
import com.sf.ams.utils.ExecutorStatusMapUtil;
import com.sf.ams.utils.GitUtil;

/**
 * 描述：从GIT中拉取代码，并获得脚本的路径
 * 
 * <pre>HISTORY
 * ****************************************************************************
 *  ID   DATE           PERSON          REASON
 *  1    2017年10月25日      Simba.Hua         Create
 * ****************************************************************************
 * </pre>
 * @author Simba.Hua
 */
public class GitSqlFileHandler extends BaseSqlFileHandler {

	public GitSqlFileHandler(List<Environment> environments, SystemInfo system) {
		super(environments, system);
	}

	@Override
	protected boolean writeFileToLocal() {
		boolean updateCodeFlag = GitUtil.pullBranchToLocal(system);
		if( !updateCodeFlag ) {
			environments.forEach(e -> 
			ExecutorStatusMapUtil.setExecutorStauts(system.getSystemCode() + "_" + e.getEnvironmentType(), SystemExceptionType.GITEXCEPTION)
			);
			return false;
		}
		return true;
	}

	@Override
	protected String sqlCodePath() {
		return  BASE_SQLCODE_PATH + 
				system.getSystemCode() +
				Constant.PATH_SPLIT_CHARACTOR + 
				Constant.GIT_SQL_PATH +
				Constant.PATH_SPLIT_CHARACTOR + 
				system.getSqlPath();
	}

}
